package com.situ.vivamarket.controller.sys;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import com.situ.vivamarket.intcp.AdminLoginInterceptor;
import com.situ.vivamarket.model.Admin;
import com.situ.vivamarket.model.AdminLogin;
import com.situ.vivamarket.service.AdminService;
import com.situ.vivamarket.util.JsonResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/vm/sys")
public class IndexController {
    private AdminService adminService;
    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @GetMapping("/index")
    public String index() {
        return "sys/common/index";
    }
    @GetMapping("/account")
    public String pageSettings1() {
        return "sys/common/account";
    }

    @GetMapping("/login")
    public String login() {
        return "sys/common/login";
    }
    @GetMapping("/review")
    public String review() {
        return "sys/common/review";
    }
    @GetMapping("/register")
    public String register() {
        return "sys/common/register";
    }
    @GetMapping("/captcha")
    public void captcha(HttpServletRequest req, HttpServletResponse resp) throws IOException { //响应图片验证码
        // 创建 GIF 动态验证码
        GifCaptcha captcha = CaptchaUtil.createGifCaptcha(100, 45, 4);
        req.getSession().setAttribute("captcha_code", captcha.getCode());

        // 设置响应头
        resp.setContentType("image/jpeg");
        // 清空缓存
        resp.setHeader("Pragma", "No-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);

        // 将验证码图片写入响应流
        captcha.write(resp.getOutputStream());
    }
//    账号密码校验
    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<JsonResult>  login(@Validated  AdminLogin adminLogin,HttpSession session) {
        String correctCapt = (String) session.getAttribute("captcha_code");
        if (correctCapt == null) {
            return ResponseEntity.badRequest().body(JsonResult.fail("验证码已过期，请重新获取"));
        }
        if(!adminLogin.getCaptcha().equals(correctCapt)) { //    验证码校验
            return ResponseEntity.badRequest().body(JsonResult.fail("验证码不正确"));
        }else {
            Admin admin = adminService.findByName(adminLogin.getUsername());
            if(admin == null) {
                return ResponseEntity.ok().body(JsonResult.fail("账户不存在"));
            }else{
                PasswordEncryptor pe=new StrongPasswordEncryptor();
                if(!pe.checkPassword(adminLogin.getPassword(),admin.getPassword())) {
                    return ResponseEntity.ok().body(JsonResult.fail("密码不正确"));
                }
                else{
                    session.setAttribute(AdminLoginInterceptor.ADMIN_LOGIN, admin);
                    return ResponseEntity.ok().body(JsonResult.successData(admin));
                }
            }
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute(AdminLoginInterceptor.ADMIN_LOGIN);
        return "sys/common/login";
    }
}
